import yaml
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    def __init__(self, driver: webdriver = None):
        if driver is None:
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(15)

        else:
            self.driver = driver

    def find_ele(self, by, locate):
        ele = self.driver.find_element(by, locate)
        return ele

    def find_ele_and_sendkeys(self, by, locate, key):
        key = self.driver.find_element(by, locate).sendkeys(key)
        return key

    def find_ele_and_click(self, by, locate):
        self.driver.find_element(by, locate).click()

    def find_eles(self, by, locate):
        eles = self.driver.find_elements(by, locate)
        return eles

    def open_url(self, url):
        self.driver.get(url)

    def wait_locate(self, by, locate, timeout=10):
        ele = WebDriverWait(self.driver, timeout).until(
            expected_conditions.visibility_of_element_located((by, locate)))
        return ele

    def close_win(self):
        self.driver.quit()
